package com.example._9_springmvc.xzy.dao.impl;

import com.example._9_springmvc.xzy.dao.UserDao;
import org.springframework.stereotype.Component;

import java.sql.*;

@Component
public class UserDaoImpl implements UserDao {
    public String UserLogin(String name, String pwd) throws ClassNotFoundException, SQLException {
        if (Verification(name, pwd)) {
            return "success";
        } else {
            return "fail";
        }
    }

    public boolean Verification(String name, String pwd) throws ClassNotFoundException, SQLException {
        //1.注册数据库的驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取数据库连接（里面内容依次是："jdbc:mysql://主机名:端口号/数据库名","用户名","登录密码"）
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/my?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "root");
        //3.需要执行的sql语句（?是占位符，代表一个参数）
        String sql = "select * from user where username='" + name + "' and password='" + pwd + "'";
        //String sql = "select * from user where username=" + name + "and password=" + pwd;
        //4.获取预处理对象，并依次给参数赋值
        Statement state = connection.createStatement();
        ResultSet re = state.executeQuery(sql);
        if (re.next()) {
            state.close();
            connection.close();
            return true;
        } else {
            state.close();
            connection.close();
            return false;
        }
    }
}
